<template>
  <div class="comment-item">
    <Avatar :userId="commentData.userId" width="50"></Avatar>
    <div class="comment-info">
      <div class="nick-name">
        <span class="name" @click="gotoUcenter(commentData.userId)">{{
          commentData.nickName
        }}</span>
        <span v-if="commentData.userId == articleUserId" class="tag-author"
          >作者</span
        >
      </div>
      <div class="comment-content">
        <div>
          <span class="tag top-topping" v-if="commentData.topType === 1"
            >置顶</span
          >
          <span class="tag no-audit" v-if="commentData.status === 0"
            >待审核</span
          >
          <span v-html="commentData.content"></span>
        </div>

        <CommentImage
          :style="{ marginTop: '10px' }"
          v-if="commentData.imgPath"
          :src="
            proxy.globalInfo.imageUrl + commentData.imgPath.replace('.', '_.')
          "
          :imgList="[proxy.globalInfo.imageUrl + commentData.imgPath]"
        ></CommentImage>
      </div>

      <div class="op-panel">
        <div class="time">
          <span class="postTime">{{ commentData.postTime }}</span>
          <span class="address">
            &nbsp;·&nbsp;{{ commentData.userIpAddress }}
          </span>
        </div>

        <span
          class="iconfont icon-good"
          @click="doLike(commentData)"
          :class="commentData.likeType == 1 ? 'active' : ''"
        >
          {{ commentData.goodCount > 0 ? commentData.goodCount : "点赞" }}</span
        >
        <span
          class="iconfont icon-comment"
          @click="showReplyPanel(commentData, 0)"
          >回复
        </span>
        <el-dropdown v-if="articleUserId == currentUserId">
          <div class="iconfont icon-more"></div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="opTop(commentData)">
                {{ commentData.topType === 0 ? "设为置顶" : "取消置顶" }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div class="comment-sub-list" v-if="commentData.children">
        <div class="comment-sub-item" v-for="sub in commentData.children">
          <Avatar :userId="sub.userId" width="30"></Avatar>
          <div class="comment-sub-info">
            <div class="nick-name">
              <span class="name" @click="gotoUcenter(sub.userId)">{{
                sub.nickName
              }}</span>
              <span v-if="sub.userId == articleUserId" class="tag-author"
                >作者</span
              >
              <span class="reply-name">回复</span>
              <span @click="gotoUcenter(sub.replyUserId)" class="a-link"
                >@{{ sub.replyNickName }}</span
              >
              <span>：</span>
              <span>{{ sub.content }}</span>
            </div>
            <div class="op-panel">
              <div class="time">
                <span class="postTime">{{ sub.postTime }}</span>
                <span class="address">
                  &nbsp;·&nbsp;{{ sub.userIpAddress }}
                </span>
              </div>

              <span class="iconfont icon-good">
                {{ sub.goodCount > 0 ? sub.goodCount : "点赞" }}</span
              >
              <span
                class="iconfont icon-comment"
                @click="showReplyPanel(sub, 1)"
                >回复
              </span>
            </div>
          </div>
        </div>
      </div>
      <div class="reply-info" v-show="commentData.showReply">
        <CommentPost
          avatarWidth="30"
          :articleId="articleId"
          :userId="currentUserId"
          :showInsertImg="false"
          :placeholderInfo="placeholderInfo"
          :pCommentId="pCommentId"
          :replyUserId="Number(replyUserId)"
          @postCommentFinish="postCommentFinish"
        ></CommentPost>
      </div>
    </div>
  </div>
</template>

<script setup>
import CommentImage from "@/components/CommentImage.vue";
import { ref, reactive, getCurrentInstance } from "vue";
import { useRouter, useRoute } from "vue-router";
const { proxy } = getCurrentInstance();
const router = useRouter();
const route = useRoute();
import CommentPost from "./CommentPost.vue";
const api = {
  doLike: "/comment/doLike",
  opTop: "/comment/changeTopType",
};
const props = defineProps({
  articleId: {
    type: String,
  },
  commentData: {
    type: Object,
    default: () => {},
  },
  articleUserId: {
    type: String,
  },
  currentUserId: {
    type: String,
  },
});
//当前回复的父id（pCommentId）
const pCommentId = ref(0);
//
const replyUserId = ref(0);
const placeholderInfo = ref(null);
const emit = defineEmits(["hiddenReplyPanel", "reloadData"]);
// 显示回复框
const showReplyPanel = (curData, type) => {
  //暂存当前回复框状态
  const haveShow =
    props.commentData.showReply == undefined
      ? false
      : props.commentData.showReply;
  emit("hiddenReplyPanel");
  if (type == 0) {
    props.commentData.showReply = !haveShow;
  } else if (type == 1) {
    props.commentData.showReply = true;
  }
  pCommentId.value = props.commentData.commentId;
  replyUserId.value = curData.userId;
  placeholderInfo.value = `回复 @` + curData.nickName + `:`;
};
//二级评论发布完成 将数据放到开头
const postCommentFinish = (resultData) => {
  props.commentData.children = resultData;
  placeholderInfo.value = void 0;
};
//跳转用户界面
const gotoUcenter = (userId) => {
  router.push(`/user/${userId}`);
};
//点赞
const doLike = async (data) => {
  const params = {
    commentId: data.commentId,
  };
  let result = await proxy.Request({
    url: api.doLike,
    params: params,
    showLoading: false,
  });
  if (!result) {
    return;
  }
  data.goodCount = result.data.goodCount;
  data.likeType = result.data.likeType;
};
//置顶
const opTop = async (data) => {
  const params = {
    commentId: data.commentId,
    topType: data.topType === 1 ? 0 : 1,
  };
  let result = await proxy.Request({
    url: api.opTop,
    params: params,
  });
  if (!result) {
    return;
  }
  emit("reloadData");
};
</script>

<style lang="scss" scoped>
.comment-item {
  display: flex;
  padding-top: 15px;
  .comment-info {
    display: flex;
    flex: 1;
    flex-direction: column;
    margin-left: 10px;
    border-bottom: 1px solid #ddd;
    padding-bottom: 20px;
    .nick-name {
      display: flex;
      align-items: center;
      .name {
        cursor: pointer;
      }
      .name {
        color: var(--text2);
        margin-right: 5px;
      }
      .tag-author {
        color: #fff;
        background: var(--pink);
        border-radius: 2px;
        font-size: 12px;
        padding: 0px 2px;
      }
    }
    .comment-content {
      font-size: 15px;
      .tag {
        margin-right: 5px;
        border-radius: 3px;
        font-size: 12px;
        padding: 0 5px;
        font-weight: bold;
      }
      .top-topping {
        color: var(--pink);
        border: 1px solid var(--pink);
      }
      .no-audit {
        color: var(--text2);
        border: 1px solid var(--text2);
      }
    }
    .op-panel {
      display: flex;
      align-items: center;
      line-height: 1;
      font-size: 13px;
      color: var(--text2);
      margin-top: 5px;
      .time {
        margin-right: 20px;
      }
      .iconfont {
        margin-right: 15px;
        font-size: 14px;
        color: var(--icon);
        cursor: pointer;
      }
      .iconfont::before {
        margin-right: 3px;
      }
      .active {
        color: var(--link);
      }
    }
    .comment-sub-list {
      margin-top: 10px;

      .comment-sub-item {
        display: flex;
        margin: 5px 0px;
        font-size: 14px;
        .comment-sub-info {
          margin-left: 5px;
          .nick-name {
            display: flex;
            align-items: center;

            .name {
              color: var(--text2);
              margin-right: 5px;
              cursor: pointer;
            }
            .tag-author {
              color: #fff;
              background: var(--pink);
              border-radius: 2px;
              font-size: 12px;
              padding: 0px 2px;
            }
            .reply-name {
              margin: 0px 5px;
              font-size: 13px;
            }
          }
        }
      }
    }
    .reply-info {
      margin-top: 15px;
    }
  }
}
</style>
